package com.inossem.warehousestorage_inossem.activity.base;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;

import com.inossem.utils.base.StatusBarCompatUtils;
import com.inossem.utils.log.LogUtils;
import com.inossem.utils.scan.ScanUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.application.WarehouseStorageApplication;
import com.inossem.warehousestorage_inossem.constant.ReceiverConstant;
import com.inossem.warehousestorage_inossem.receiver.ScanReceiver;

public class BaseActivity extends AppCompatActivity {

    private ScanReceiver mScanReceiver;

    private ScanReceiver.ScanListener mScanListener;

    private ScanUtils.ScanCodeListener mScanCodeListener;

    private String[] mQRCodeTypeArray;

    public void setmScanListener(ScanReceiver.ScanListener scanListener, String... qrCodeTypeArray) {
        this.mScanListener = scanListener;
        this.mQRCodeTypeArray = qrCodeTypeArray;
    }

    public void setmScanCodeListener(ScanUtils.ScanCodeListener scanCodeListener) {
        this.mScanCodeListener = scanCodeListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);
//            requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置无标题
//            //设置全屏
//            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            WarehouseStorageApplication.getInstance().pushActivity(this);
            LogUtils.i("当前页面ClassName【" + this.getComponentName().getClassName() + "】");
//            AppCompatActivity下设置无标题
//            getSupportActionBar().hide();
            StatusBarCompatUtils.compat(this, ContextCompat.getColor(this, R.color.include_header_background));
            onCreateImpl(savedInstanceState);
        } catch (Throwable throwable) {
            LogUtils.logThrowableE(this, "", throwable);
        }
    }

    @Override
    protected void onStart() {
        try {
            super.onStart();
            onStartImpl();
        } catch (Throwable Throwable) {
            LogUtils.logThrowableE(this, "", Throwable);
        }
    }

    @Override
    protected void onRestart() {
        try {
            super.onRestart();
            onRestartImpl();
        } catch (Throwable Throwable) {
            LogUtils.logThrowableE(this, "", Throwable);
        }
    }

    @Override
    protected void onResume() {
        try {
            super.onResume();
            onResumeImpl();
            //注册扫描广播
            registerReceiver();
        } catch (Throwable Throwable) {
            LogUtils.logThrowableE(this, "", Throwable);
        }
    }

    @Override
    protected void onPause() {
        try {
            super.onPause();
            onPauseImpl();
            //注销扫描广播
            unRegisterReceiver();
        } catch (Throwable Throwable) {
            LogUtils.logThrowableE(this, "", Throwable);
        }
    }

    @Override
    protected void onStop() {
        try {
            super.onStop();
            onStopImpl();
        } catch (Throwable Throwable) {
            LogUtils.logThrowableE(this, "", Throwable);
        }

    }

    @Override
    protected void onDestroy() {
        try {
            super.onDestroy();
            WarehouseStorageApplication.getInstance().popActivity(this);
            onDestroyImpl();
        } catch (Throwable Throwable) {
            LogUtils.logThrowableE(this, "", Throwable);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            super.onActivityResult(requestCode, resultCode, data);
            if(mScanCodeListener!=null){
                mScanCodeListener.onResult(ScanUtils.getScanResult(requestCode, resultCode, data));
            }
            onActivityResultImpl(requestCode, resultCode, data);
        } catch (Throwable Throwable) {
            LogUtils.logThrowableE(this, "", Throwable);
        }
    }

    @Override
    protected void onUserLeaveHint() {
        try {
            super.onUserLeaveHint();
            onUserLeaveHintImpl();
        } catch (Throwable Throwable) {
            LogUtils.logThrowableE(this, "", Throwable);
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        try {
            super.onWindowFocusChanged(hasFocus);
            onWindowFocusChangedImpl(hasFocus);
        } catch (Throwable Throwable) {
            LogUtils.logThrowableE(this, "", Throwable);
        }
    }

    protected void onCreateImpl(Bundle savedInstanceState) {
    }

    protected void onStartImpl() {
    }

    protected void onRestartImpl() {
    }

    protected void onResumeImpl() {
    }

    protected void onPauseImpl() {
    }

    protected void onStopImpl() {
    }

    protected void onDestroyImpl() {
    }

    protected void onActivityResultImpl(int requestCode, int resultCode, Intent data) {

    }

    protected void onUserLeaveHintImpl() {
    }

    public void onWindowFocusChangedImpl(boolean hasFocus) {
    }

    private void registerReceiver() {
        mScanReceiver = new ScanReceiver(mScanListener, mQRCodeTypeArray);
        IntentFilter scanReceiverFilter = new IntentFilter();
        scanReceiverFilter.addAction(ReceiverConstant.SCAN_ACTION);
        registerReceiver(mScanReceiver, scanReceiverFilter);
    }

    private void unRegisterReceiver() {
        if (mScanReceiver != null) {
            unregisterReceiver(mScanReceiver);
            mScanReceiver = null;
        }
    }
}
